﻿/* 
This file is part of Ingo Karstein's SharePoint PowerShell Timer Jobs project

**Do not remove this comment**

Please see the project homepage at CodePlex:
  http://sppowershelltimerjob.codeplex.com/

Please see my blog:
  http://ikarstein.wordpress.com

Licence: GPLv2
    Ingo Karstein's SharePoint PowerShell Timer Jobs
    Copyright (C) 2011  Ingo Karstein, ikarstein@hotmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;

namespace ik.SharePoint2010.SPPowerShellTimerJob.Layouts.SPPowerShellTimerJob
{
    public partial class EditScript : ManageJobs
    {
        private Guid currentJobGuid = Guid.Empty;
        private SPJobDefinition currentJob = null;

        protected new void Page_Load(object sender, EventArgs e)
        {
            btnSave.Click += new EventHandler(btnSave_Click);
            btnSaveClose.Click += new EventHandler(btnSaveClose_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);

            if( string.IsNullOrEmpty(Request["jobdefid"]) )
            {
                currentJobGuid = Guid.Empty;
            }
            else
            {
                currentJobGuid = new Guid(Request["jobdefid"]);
            }

            if( !Guid.Equals(currentJobGuid, Guid.Empty) )
                currentJob = FindJobDef(currentJobGuid);
            else
                currentJob = null;

            if( currentJob == null )
            {
                Close();
            }

            if( !IsPostBack )
            {
                if( !string.IsNullOrEmpty((string)currentJob.Properties["PSScript"]) )
                {
                    script.Text = currentJob.Properties["PSScript"] as string;
                }
                else
                {
                    script.Text = scripttemplate;
                }
            }
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Save()
        {
            currentJob.Properties["PSScript"] = script.Text;
            currentJob.Update();
        }

        private void Close()
        {
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "close", @"window.frameElement.commitPopup();", true);
        }

        void btnSaveClose_Click(object sender, EventArgs e)
        {
            Save();
            Close();
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }
    }
}
